import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/constant/color.dart';

enum ImageStatus {
  FILE,
  NETWORK,
  MEMORY,
  ASSET,
}

class ImageWidget extends StatelessWidget {

  final ImageStatus? imageStatus;
  final String? imageUrl;
  final String? imageFilePath;
  final String? imageAssetPath;
  final Uint8List? thumb;
  final double? width;
  final double? height;
  final double? diameter;
  final BoxFit fit;
  final Function()? onTap;
  final bool isCircle;
  final BorderRadius? borderRadius;
  final double? radius;
  final Color? backGroundColor;
  final LoadingErrorWidgetBuilder? errorWidgetBuilder;
  final PlaceholderWidgetBuilder? placeholderBuilder;
  final Duration fadeInDuration;
  final bool isShowErrorWidget;
  final Widget? errorWidget;
  final int? cacheWidth;
  final int? cacheHeight;
  final bool isGaussianBlur;

  ImageWidget.network(
      this.imageUrl,
      {this.width,
      this.height,
      this.diameter,
      this.onTap,
      this.fit=BoxFit.cover,
      this.isCircle=false,
      this.borderRadius,
      this.radius,
      this.backGroundColor,
      this.errorWidgetBuilder,
      this.placeholderBuilder,
      this.fadeInDuration=Duration.zero,
      this.isShowErrorWidget=true,
      this.isGaussianBlur=false,
      this.errorWidget,
      this.cacheWidth,
      this.cacheHeight,
  }):this.thumb=null,
    this.imageFilePath=null,
    this.imageAssetPath=null,
    this.imageStatus=ImageStatus.NETWORK;


  ImageWidget.file(
      this.imageFilePath,
      {this.width,
      this.height,
      this.diameter,
      this.onTap,
      this.fit=BoxFit.cover,
      this.isCircle=false,
      this.borderRadius,
      this.radius,
      this.backGroundColor,
      this.errorWidgetBuilder,
      this.placeholderBuilder,
      this.fadeInDuration=Duration.zero,
      this.isShowErrorWidget=true,
      this.isGaussianBlur=false,
      this.errorWidget,
      this.cacheWidth,
      this.cacheHeight,
  }):this.thumb=null,
    this.imageUrl=null,
    this.imageAssetPath=null,
    this.imageStatus=ImageStatus.FILE;


  ImageWidget.memory(
      this.thumb,
      {this.width,
      this.height,
      this.diameter,
      this.onTap,
      this.fit=BoxFit.cover,
      this.isCircle=false,
      this.borderRadius,
      this.radius,
      this.backGroundColor,
      this.errorWidgetBuilder,
      this.placeholderBuilder,
      this.fadeInDuration=Duration.zero,
      this.isShowErrorWidget=true,
      this.isGaussianBlur=false,
      this.errorWidget,
      this.cacheWidth,
      this.cacheHeight,
  }):this.imageUrl=null,
    this.imageAssetPath=null,
    this.imageFilePath=null,
    this.imageStatus=ImageStatus.MEMORY;



  ImageWidget.asset(
      this.imageAssetPath,
      {this.width,
      this.height,
      this.diameter,
      this.onTap,
      this.fit=BoxFit.cover,
      this.isCircle=false,
      this.borderRadius,
      this.radius,
      this.backGroundColor,
      this.errorWidgetBuilder,
      this.placeholderBuilder,
      this.fadeInDuration=Duration.zero,
      this.isShowErrorWidget=true,
      this.isGaussianBlur=false,
      this.errorWidget,
      this.cacheWidth,
      this.cacheHeight,
  }):this.thumb=null,
    this.imageUrl=null,
    this.imageFilePath=null,
    this.imageStatus=ImageStatus.ASSET;



  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: _isHaveClipRRect(),
      onTap: onTap,
    );
  }

  Widget _isHaveClipRRect(){
    if(borderRadius!=null){
      return ClipRRect(
        borderRadius: borderRadius,
        child: _getBody(),
      );
    }else if(radius!=null){
      return ClipRRect(
        borderRadius: BorderRadius.circular(radius!),
        child: _getBody(),
      );
    }else if(isCircle){
      return ClipRRect(
        borderRadius: BorderRadius.circular(1000),
        child: _getBody(),
      );
    }else{
      return _getBody();
    }
  }

  Widget _getBody(){
    return Container(
      width: _getWidth(),
      height: _getHeight(),
      color: _getBackGroundColor(),
      child: _getGaussianBlur(),
    );
  }
  //高斯模糊
  Widget _getGaussianBlur(){
    if(isGaussianBlur){
      return Container(
        child:  Stack(
          children: [
            Container(
              color: Colors.white.withOpacity(0.1),
              width:double.infinity,
              height: double.infinity,
              child: _getImageWidget(),
            ),
            BackdropFilter(
              filter: ImageFilter.blur(sigmaX: 4, sigmaY: 4),
              child: Container(
                color: Colors.white.withOpacity(0.1),
                width:double.infinity,
                height: double.infinity,
              ),
            )
          ],
        ),
      );
    }else{
      return _getImageWidget();
    }
  }

  Widget _getImageWidget(){
    switch(imageStatus){
      case ImageStatus.ASSET:
        return _getAssetImage();
      case ImageStatus.FILE:
        return _getFileImage();
      case ImageStatus.NETWORK:
        return _getNetworkImage();
      case ImageStatus.MEMORY:
        return _getImageMemory();
      default:
        return _getErrorWidget();
    }
  }



  Widget _getAssetImage(){
    return Image.asset(
      imageAssetPath??"",
      width: _getWidth(),
      height: _getHeight(),
      fit: fit,
      frameBuilder: _getFrameBuilder(),
      errorBuilder: _getErrorBuilder(),
      cacheWidth: _getCacheWidth(),
      cacheHeight: _getCacheHeight(),
    );
  }

  Widget _getFileImage(){
    File videoImageFile = File(imageFilePath??"");
    return Image.file(
      videoImageFile,
      width: _getWidth(),
      height: _getHeight(),
      fit: fit,
      frameBuilder: _getFrameBuilder(),
      errorBuilder: _getErrorBuilder(),
      cacheWidth: _getCacheWidth(),
      cacheHeight: _getCacheHeight(),
    );
  }

  Widget _getNetworkImage(){
    return CachedNetworkImage(
      width: _getWidth(),
      height: _getHeight(),
      imageUrl: imageUrl ?? "",
      fit: fit,
      fadeInDuration: fadeInDuration,
      placeholder: placeholderBuilder,
      errorWidget: _getLoadingErrorBuilder(),
      memCacheWidth: _getCacheWidth(),
      memCacheHeight: _getCacheHeight(),
    );
  }

  Widget _getImageMemory() {
    return Image.memory(
      thumb??Uint8List(0),
      width: _getWidth(),
      height: _getHeight(),
      fit: fit,
      frameBuilder: _getFrameBuilder(),
      errorBuilder: _getErrorBuilder(),
      cacheWidth: _getCacheWidth(),
      cacheHeight: _getCacheHeight(),
    );
  }

  Widget _getErrorWidget() {
    if(isShowErrorWidget){
      if(errorWidget==null){
        return Container(
          width: _getWidth(),
          height: _getHeight(),
          color: AppColor.bgWhite,
          alignment: Alignment.center,
          child: Text(
            "文件损坏",
            style: TextStyle(fontSize: 12, color: AppColor.black),
          ),
        );
      }else{
        return errorWidget!;
      }
    }else{
      return Container();
    }
  }


  ImageFrameBuilder? _getFrameBuilder(){
    if(placeholderBuilder==null){
      return null;
    }else{
      return _frameBuilder;
    }
  }

  Widget _frameBuilder (BuildContext context, Widget child, int? frame, bool wasSynchronouslyLoaded){
    return placeholderBuilder!(context,_getImageUrl());
  }


  ImageErrorWidgetBuilder? _getErrorBuilder() {
    if (errorWidgetBuilder == null) {
      if (isShowErrorWidget) {
        return _errorDefaultBuilder;
      }
      return null;
    } else {
      return _errorBuilder;
    }
  }

  LoadingErrorWidgetBuilder? _getLoadingErrorBuilder() {
    if (errorWidgetBuilder == null) {
      if (isShowErrorWidget) {
        return (context, url, error) => Container(
              child: _getErrorWidget(),
            );
      }
      return null;
    } else {
      return errorWidgetBuilder;
    }
  }

  Widget _errorBuilder(BuildContext context, Object error, StackTrace? stackTrace) {
    return errorWidgetBuilder!(context, _getImageUrl(), error);
  }

  Widget _errorDefaultBuilder(BuildContext context, Object error, StackTrace? stackTrace) {
    return _getErrorWidget();
  }

  Color? _getBackGroundColor() {
    if (backGroundColor != null && backGroundColor is Color) return backGroundColor!;
    if (onTap != null) return AppColor.transparent;
    return null;
  }

  String _getImageUrl(){
    switch(imageStatus){
      case ImageStatus.ASSET:
        return imageAssetPath??"imageAssetPath";
      case ImageStatus.FILE:
        return imageFilePath??"imageFilePath";
      case ImageStatus.NETWORK:
        return imageUrl??"imageUrl";
      case ImageStatus.MEMORY:
        return "Uint8List格式";
      default:
        return "未知格式";
    }
  }


  double? _getWidth(){
    if(width!=null&&width!>0){
      return width;
    }else if(diameter!=null&&diameter!>0){
      return diameter;
    }else{
      return null;
    }
  }
  double? _getHeight(){
    if(height!=null&&height!.toInt()>0){
      return height;
    }else if(diameter!=null&&diameter!>0){
      return diameter;
    }else{
      return null;
    }
  }

  int? _getCacheWidth(){
    if(cacheWidth!=null&&cacheWidth!>0){
      return cacheWidth;
    }else if(width!=null&&width!.toInt()>0){
      return width!.toInt();
    }else if(diameter!=null&&diameter!.toInt()>0){
      return diameter!.toInt();
    }else{
      return null;
    }
  }

  int? _getCacheHeight(){
    if(cacheHeight!=null&&cacheHeight!>0){
      return cacheHeight;
    }else if(height!=null&&height!.toInt()>0){
      return height!.toInt();
    }else if(diameter!=null&&diameter!.toInt()>0){
      return diameter!.toInt();
    }else{
      return null;
    }
  }
}
